package OptimalAlgorithm.Simulation;

import java.util.HashMap;

//https://leetcode.cn/problems/minimum-number-of-frogs-croaking/
public class MinNumberOfFrogs {
/*    public int minNumberOfFrogs(String croakOfFrogs) {
        int len = croakOfFrogs.length();
        char[] ch = croakOfFrogs.toCharArray();
        HashMap<Character,Integer> map = new HashMap<>();
        map.put('c',0);
        map.put('r',0);
        map.put('o',0);
        map.put('a',0);
        map.put('k',0);
        for (int i = 0; i < len; i++) {
            if(ch[i] == 'c'){
                if(map.get('k') == 0){
                    map.put('c', map.get('c') + 1);
                }else {
                    map.put('k', map.get('k') - 1);
                    map.put('c', map.get('c') + 1);
                }
            }
            if(ch[i] == 'r'){
                if(map.get('c') == 0){
                    return -1;
                }else {
                    map.put('r',map.get('r') + 1);
                    map.put('c',map.get('c') - 1);
                }
            }
            if(ch[i] == 'o'){
                if(map.get('r') == 0){
                    return -1;
                }else {
                    map.put('o',map.get('o') + 1);
                    map.put('r',map.get('r') - 1);
                }
            }
            if(ch[i] == 'a'){
                if(map.get('o') == 0){
                    return -1;
                }else {
                    map.put('a',map.get('a') + 1);
                    map.put('o',map.get('o') - 1);
                }
            }
            if(ch[i] == 'k') {
                if (map.get('a') == 0) {
                    return -1;
                } else {
                    map.put('k', map.get('k') + 1);
                    map.put('a', map.get('a') - 1);
                }
            }
        }
        if (map.get('c') != 0 || map.get('r') != 0 || map.get('o') != 0 || map.get('a') != 0) {
            return -1;
        }
        return map.get('k');
    }*/
    public int minNumberOfFrogs(String croakOfFrogs) {
        String yell = "croak";
        int len = croakOfFrogs.length();
        char[] ch = croakOfFrogs.toCharArray();
        int[] hash = new int[yell.length()];//用数组来模拟hash表
        HashMap<Character,Integer> map = new HashMap<>();//用来对应yell叫声的字母的下标
        for (int i = 0; i < yell.length(); i++) {
            map.put(yell.charAt(i),i);
        }
        for (int i = 0; i < len; i++) {
            if(ch[i] == yell.charAt(0)){
                if(hash[yell.length() - 1] != 0){
                    hash[yell.length() - 1]--;
                }
                hash[0]++;
            }else {
                if(hash[map.get(ch[i]) - 1] == 0){
                    return -1;
                }else {
                    hash[map.get(ch[i]) - 1]--;
                    hash[map.get(ch[i])]++;
                }
            }
        }
        for (int i = 0; i < yell.length() - 1; i++) {
            if(hash[i] != 0){
                return -1;
            }
        }
        return hash[yell.length() - 1];
    }
}
